package com.jx.mst.mst.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.jx.mst.model.common.dtos.PageParams;
import com.jx.mst.model.common.dtos.ResponseResult;
import com.jx.mst.model.mst.dtos.MstSelDtos;
import com.jx.mst.model.mst.dtos.MstSelInfoDtos;
import com.jx.mst.model.mst.reps.MstSelReps;
import com.jx.mst.mst.entity.MstSel;
import com.jx.mst.mst.service.MstSelService;
import jakarta.annotation.Resource;
//import org.springframework.data.domain.Page;
//import org.springframework.data.domain.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.jx.mst.model.common.redisCon.MseRedisCon.*;


/**
 * 面试题分类(MstSel)表控制层
 *
 * @author makejava
 * @since 2023-12-25 07:13:07
 */
@RestController
@RequestMapping("mstSel")
public class MstSelController {
    /**
     * 服务对象
     */
    @Resource
    private MstSelService mstSelService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostMapping("/getMstSel")
    public ResponseResult getMstSel(@RequestBody(required = true) PageParams pageParams){
//        List<MstSel> list = mstSelService.query().orderByAsc("mst_sel_sort").list();
        return mstSelService.queryByPage(pageParams);
    }

    @GetMapping("/getMstSelById")
    public ResponseResult getMstSelById(@RequestParam(required = true) Long id){
//        List<MstSel> list = mstSelService.query().orderByAsc("mst_sel_sort").list();
        MstSel mstSel = mstSelService.getById(id);
        return ResponseResult.okResult(mstSel);
    }

    @GetMapping("/getMstSelList")
    public ResponseResult getMstSelList(){
        List<MstSel> list = mstSelService.query().orderByAsc("mst_sel_sort").list();
//        MstSel mstSel = mstSelService.getById(id);
        return ResponseResult.okResult(list);
    }

    @PostMapping("/addMstSel")
    public ResponseResult addMstSel(@RequestBody(required = true) MstSelDtos mstSelDtos) {
        MstSel mstSel = BeanUtil.copyProperties(mstSelDtos, MstSel.class);
        mstSel.setMstSelCreatetime(new Date());
//        mstSel.setId(Long.valueOf(RandomUtil.randomInt(8)));
        boolean save = mstSelService.save(mstSel);
        stringRedisTemplate.opsForZSet().add(REDIS_ZSET_MSTSEL,mstSel.getId().toString() , 1);
        stringRedisTemplate.opsForValue().set(REDIS_PREX_MSTSEL+mstSel.getId().toString(),"0");
        return ResponseResult.okResult("添加成功");
    }

    @PostMapping("/editMstSel")
    public ResponseResult editMstSel(@RequestBody(required = true) MstSel mstSel) {
//        MstSel mstSel = BeanUtil.copyProperties(mstSelDtos, MstSel.class);
//        mstSel.setMstSelCreatetime(new Date());
//        mstSel.setId(RandomUtil.randomLong());
//        boolean save = mstSelService.save(mstSel);
        boolean update = mstSelService.updateById(mstSel);
        return ResponseResult.okResult("修改成功");
    }

    @DeleteMapping("/delMstSel")
    public ResponseResult delMstSel(Long[] ids ) {
//        mstSel.setMstSelCreatetime(new Date());
        List<Long> collect = Arrays.stream(ids).collect(Collectors.toList());
        collect.forEach(item -> {
            stringRedisTemplate.opsForZSet().remove(REDIS_ZSET_MSTSEL,item.toString());
            stringRedisTemplate.delete(REDIS_PREX_MSTSEL+item.toString());
        });
        boolean del = mstSelService.removeByIds(collect);
        return ResponseResult.okResult("删除成功");
    }

    @GetMapping("/getHomeMstSel")
    public ResponseResult getHomeMstSel(){
        List<MstSelReps> list = mstSelService.getHomeMstSel();
        return ResponseResult.okResult(list);
    }

    @GetMapping("/getMstselList")
    public ResponseResult getMstselList(){
        List<MstSel> list = mstSelService.getMstselList();
        return ResponseResult.okResult(list);
    }

    @GetMapping("/getMstelandMstinfoByMstselId")
    public ResponseResult getMstelandMstinfoByMstselId(@RequestParam(value = "mstselId")String mstselId){
        MstSelInfoDtos mstSelInfo = mstSelService.getMstelandMstinfoByMstselId(mstselId);
        return ResponseResult.okResult(mstSelInfo);
    }



}

